//
//  ByCellGoodsList.m
//  mijifen_ios
//
//  Created by mc on 2017/11/6.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellGoodsList.h"

@implementation ByCellGoodsList
{
    UIImageView     *_goodImg;
    UILabel         *_desLabel;
    UILabel         *_priceLabel;
    UILabel         *_stockLabel;
    UILabel         *_soldLabel;        // 已售
    UILabel         *_statusLabel;      // 状态（是否推荐）
    UIView          *_lineView;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellGoodsList";
    ByCellGoodsList *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _goodImg  = [[UIImageView alloc] init];
    [self addSubview:_goodImg];
    
    _desLabel = [[UILabel alloc] init];
    _desLabel.numberOfLines = 2;
    [self addSubview:_desLabel];
    
    _priceLabel = [[UILabel alloc] init];
    [self addSubview:_priceLabel];
    
    _stockLabel = [[UILabel alloc] init];
    [self addSubview:_stockLabel];
    
    _soldLabel = [[UILabel alloc] init];
    [self addSubview:_soldLabel];
    
    _statusLabel = [[UILabel alloc] init];
    [self addSubview:_statusLabel];
    _statusLabel.textColor = BY_TEXT_COLOR;
    _statusLabel.textAlignment = NSTextAlignmentCenter;
    _statusLabel.layer.cornerRadius = 4.0;
    _statusLabel.layer.borderColor = BY_TEXT_COLOR.CGColor;
    _statusLabel.layer.borderWidth = 1;
    
    _lineView = [[UIView alloc] init];
    [self addSubview:_lineView];
}

- (void)setModel:(ByModelGoodsList *)model{
    _model = model;
    
    [_goodImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(140, 140));
    }];
    _goodImg.backgroundColor = [UIColor redColor];
    
    [_desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_goodImg.mas_right).offset(10);
        make.top.equalTo(_goodImg.mas_top);
        make.right.equalTo(@(-10));
        make.height.mas_equalTo(@(50));
    }];
    _desLabel.text = _model.describe;
    _desLabel.font = FONT(16);
    
    CGFloat width = [@"已推荐" getSizeOfString:16].width + 20;
    [_statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_desLabel.mas_bottom).offset(5);
        make.left.equalTo(_priceLabel.mas_right);
        make.right.equalTo(@(-5));
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    if (_model.status.integerValue == 1) {
        _statusLabel.hidden = NO;
    }else{
        _statusLabel.hidden = YES;
    }
    _statusLabel.text = @"已推荐";
    
    NSString *price = [NSString stringWithFormat:@"¥%@",_model.price];
    width = [price getSizeOfString:16].width + 10;
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_desLabel.mas_left);
        make.top.equalTo(_desLabel.mas_bottom).offset(5);
        make.right.equalTo(_statusLabel.mas_left).offset(-5);
        make.height.mas_equalTo(@(30));
//        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _priceLabel.font = FONT(16);
    _priceLabel.textColor = [UIColor redColor];
    _priceLabel.text = price;
    
 

    
    [_stockLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceLabel.mas_left);
        make.top.equalTo(_priceLabel.mas_bottom).offset(5);
        make.right.equalTo(@(-10));
        make.height.equalTo(@(30));
    }];
    _stockLabel.text = [NSString stringWithFormat:@"库存：%@",_model.stock];
    _stockLabel.font = FONT(16);
    
    [_soldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-10));
        make.bottom.equalTo(self.mas_bottom).offset(-10);
        make.left.equalTo(_priceLabel.mas_left);
        make.height.equalTo(@(30));
    }];
    _soldLabel.text = [NSString stringWithFormat:@"已售：%@",_model.sold];
    _soldLabel.font = FONT(16);
    _soldLabel.textAlignment = NSTextAlignmentRight;
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(0));
        make.left.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 20, 1));
    }];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    
    
}

@end
